/** 仓储档案 */

import React, {useEffect, useState} from 'react'
import {Button, Space, message, TreeSelect} from 'antd'
import type {ColumnsType} from 'antd/es/table'
import {history, connect} from 'umi'
import {ArrowDownOutlined} from '@ant-design/icons'
import styles from './index.less'
import SearchForm from '@/components/SearchForm'
import Permissions from '@/components/Permissions'
import {translateDataToTree} from '@/utils/common'
import {getCompany} from '@/service/common'
import CustomTable from '@/components/Table'
import {getAmcparkingstorageList} from '@/service/storage/parkingLot'
import {getAllParking} from '@/service/storage/archives'
import {downloadDocumentUrl} from '@/service/common/download'

interface DataType {
	warehouseName: string | string[] | null
	id: string
	warehouseCode: string
	totalStorageLocations: number
	usedStorageLocations: number
	freeStorageLocations: number
	orgId: number
	cityName: string
}

const formItemLayout = {
	labelCol: {flex: '80px'},
	wrapperCol: {flex: '1'},
}

const fieldNames = {
	label: 'name',
	value: 'id',
	id: 'id',
	pId: 'parentId',
}

const warehouseLocation = (props: any) => {
	const [dataSourse, setDataSourse] = useState<any[]>([])
	const [searchData, setSearchData] = useState({})
	const [companyData, setCompanyData] = useState([])
	const [warehouseData, setWarehouseData] = useState<any>([])
	const [loading, setLoading] = useState(false)
	const [total, setTotal] = useState()
	const [params, setParams] = useState({
		current: 1,
		size: 10,
	})

	/** 获取所属公司数据 */
	const onCompany = () => {
		getCompany({merchantId: props?.user?.orgId}).then((res: any) => {
			const list = translateDataToTree(res.data)
			setCompanyData(list as any)
		})
	}
	useEffect(() => {
		onCompany()
	}, [props?.user?.orgId])
	// 获取库位档案列表
	const getData = () => {
		setLoading(true)
		getAmcparkingstorageList({...params, ...searchData})
			.then((res: any) => {
				if (res?.success) {
					setDataSourse(res?.data?.records)
					setTotal(res?.data?.total)
					setLoading(false)
				} else {
					setLoading(false)
				}
			})
			.catch((err) => {
				setDataSourse([])
				message.error(err)
				setLoading(false)
			})
	}
	useEffect(() => {
		getData()
	}, [params])
	useEffect(() => {
		/** 获取停车场列表 */
		getAllParking({warehouseType: 1})
			.then((res: any) => {
				setWarehouseData(res.data)
			})
			.catch(() => {
				setWarehouseData([])
			})
	}, [])

	/** 点击详情及编辑事件 */
	const onDetails = (type: string, record: DataType) => {
		history.push({
			pathname: `/storage/parkingLot/warehouseLocation/${type}`,
			query: {
				id: record?.id,
				name: record?.warehouseName,
			},
		})
	}

	const columns: ColumnsType<DataType> = [
		{
			title: '停车场代码',
			dataIndex: 'warehouseCode',
			align: 'center',
			width: 150,
		},
		{
			title: '停车场名称',
			dataIndex: 'warehouseName',
			align: 'center',
			width: 120,
		},
		{
			title: '库位总数',
			dataIndex: 'totalStorageLocations',
			align: 'center',
			width: 100,
		},
		{
			title: '已占用数量',
			dataIndex: 'usedStorageLocations',
			align: 'center',
			width: 120,
		},
		{
			title: '空置数量',
			dataIndex: 'freeStorageLocations',
			align: 'center',
			width: 100,
		},
		{
			title: '所属公司',
			dataIndex: 'orgId',
			align: 'center',
			width: 150,
			render: (text) => (
				<>
					{text ? (
						<TreeSelect
							value={text}
							disabled
							dropdownStyle={{maxHeight: 400, overflow: 'auto'}}
							treeDefaultExpandAll
							fieldNames={fieldNames}
							treeDataSimpleMode
							treeData={companyData}
						/>
					) : (
						'-'
					)}
				</>
			),
		},
		{
			title: '城市',
			dataIndex: 'cityName',
			align: 'center',
			width: 100,
		},
		{
			title: '操作',
			key: 'action',
			align: 'center',
			fixed: 'right',
			width: 120,
			render: (text: any, record) => (
				<Space size={12}>
					<Permissions permissionID="amc_storage_parkingLot_warehouseLocation_view">
						<Button
							type="link"
							onClick={() => {
								onDetails('view', record)
							}}>
							详情
						</Button>
					</Permissions>
					<Permissions permissionID="amc_storage_parkingLot_warehouseLocation_edit">
						<Button
							type="link"
							onClick={() => {
								onDetails('edit', record)
							}}>
							编辑
						</Button>
					</Permissions>
				</Space>
			),
		},
	]
	const formItems: any[] = [
		{
			type: 'group',
			key: 'group1',
			groupItems: [
				{
					type: 'select',
					key: 'id',
					name: 'id',
					label: '停车场',
					keys: 'id',
					title: 'warehouseName',
					items: warehouseData,
				},
				{},
				{},
			],
		},
	]
	const onFinish = (values: any) => {
		setSearchData(values)
		setParams({
			current: 1,
			size: 10,
		})
	}

	const onReset = () => {
		setSearchData({})
		setParams({
			current: 1,
			size: 10,
		})
	}

	return (
		<div className={styles.warehouseLocation}>
			<div className={styles.content}>
				<div className={styles.search}>
					<SearchForm
						onFinish={onFinish}
						onReset={onReset}
						formItems={formItems}
						formLayout={formItemLayout}
					/>
				</div>
				<div className={styles.btnlist}>
					<Space size={12}>
						<Permissions permissionID="amc_storage_parkingLot_warehouseLocation_export">
							<Button
								icon={<ArrowDownOutlined />}
								className={styles.import}
								ghost
								size="small"
								onClick={() => {
									downloadDocumentUrl({
										resourceId: '136',
										queryDesc:
											'停车场库位档案导出,resourceId:136',
										queryParam: searchData,
									})
								}}>
								导出
							</Button>
						</Permissions>
					</Space>
				</div>
				<CustomTable
					params={params}
					columns={columns}
					total={total}
					pagination
					callback={(pagination: any) => {
						setParams(pagination)
					}}
					loading={loading}
					dataSource={dataSourse}
				/>
			</div>
		</div>
	)
}
export default connect(({user}: any) => ({user}))(warehouseLocation)
